package com.btl.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StreamTools {

	public static byte[] getByteArrayData(InputStream inStream)
			throws IOException {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) > 0) {
			outStream.write(buffer, 0, len);
		}
		inStream.close();

		return outStream.toByteArray();
	}

	public static String getString(InputStream inStream) throws IOException {

		BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
		String line = null;
		StringBuffer sb = new StringBuffer();
		while ((line = in.readLine()) != null) {
			sb.append(line);
		}

		return sb.toString();
	}

	public static String getStringGb2312(InputStream inStream)
			throws IOException {

		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) > 0) {
			outStream.write(buffer, 0, len);
		}
		inStream.close();

		return new String(outStream.toByteArray(), "GB2312");
	}

}
